﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CarInfor.DAL;

namespace CarInfor.UI
{
    public partial class frmInsuranceItemEdit : Form
    {

        string id = "";
        ActionType curActionType;
        public frmInsuranceItemEdit(string strid, ActionType actionType)
        {
            InitializeComponent();
            curActionType = actionType;
            id = strid;
            if (actionType == ActionType.Edit)
            {
                InsuranceItem it = new InsuranceItem();
                it.QueryWithKey(strid);
                this.txtName.Text = it.ItemName;
                this.txtInsuredAmount.Text = it.InsuredAmount.ToString("0.00");
                this.txtPrice.Text = it.Price.ToString("0.00");
                this.Text = "修改";
            }
            else
            {
                this.Text = "增加";
            }
            this.txtName.SelectionStart = this.txtName.Text.Length;
        }

        bool checkInput()
        {
            if (this.txtName.Text.Trim() == "")
            {
                MessageHelper.ShowInfo("请输入名称！");
                return false;
            }

            try
            {
                if (string.IsNullOrEmpty(this.txtInsuredAmount.Text.Trim()))
                {
                    this.txtInsuredAmount.Text = "0.00";
                }
                Decimal.Parse(this.txtInsuredAmount.Text);
            }
            catch
            {
                MessageHelper.ShowInfo("保额输入有误！");
                this.txtInsuredAmount.Focus();
                return false;
            }

            try
            {
                Decimal.Parse(this.txtPrice.Text);
            }
            catch
            {
                MessageHelper.ShowInfo("金额输入有误！");
                this.txtPrice.Focus();
                return false;
            }

            return true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!checkInput())
                return;
            if (curActionType == ActionType.Edit)
            {
                try
                {
                    InsuranceItem item = new InsuranceItem();
                    item.ItemName = this.txtName.Text.Trim();
                    item.InsuredAmount = decimal.Parse(this.txtInsuredAmount.Text.Trim());
                    item.Price = decimal.Parse(this.txtPrice.Text.Trim());
                    item.UpdateWithKey(id);
                    this.DialogResult = DialogResult.OK;
                }
                catch
                {
                    MessageHelper.ShowFail("修改失败！");
                    this.DialogResult = DialogResult.Cancel;
                }
            }
            else
            {
                try
                {
                    InsuranceItem item = new InsuranceItem();
                    item.ID = Guid.NewGuid().ToString();
                    item.ItemName = this.txtName.Text.Trim();
                    item.InsuredAmount = decimal.Parse(this.txtInsuredAmount.Text.Trim());
                    item.Price = decimal.Parse(this.txtPrice.Text.Trim());
                    item.Insert();
                    this.DialogResult = DialogResult.OK;
                }
                catch
                {
                    MessageHelper.ShowFail("添加失败！");
                    this.DialogResult = DialogResult.Cancel;
                }

            }


        }

        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46)
            {

                e.Handled = true;
            }
            else if (e.KeyChar == 46 && txtPrice.Text.IndexOf(".") >= 0)
            {
                e.Handled = true;
            }
            else if (e.KeyChar == 13)
            {
                txtCsJe_Validating(sender, new CancelEventArgs());
            }
        }

        private void txtCsJe_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            try
            {
                Decimal.Parse(tb.Text);
            }
            catch
            {
                return;
            }

            if (tb.Text.IndexOf(".") == -1)
            {
                tb.Text = tb.Text + ".00";
                return;
            }
            else if (tb.Text.IndexOf(".") == 0)
            {
                tb.Text = "0" + tb.Text;
            }
            else
            {
                int i = tb.Text.IndexOf(".") + 3;
                tb.Text = tb.Text.Substring(0, i < tb.Text.Length ? i : tb.Text.Length);
            }
            int clen = tb.Text.Length;
            for (int i = 0; i < 3 - (clen - tb.Text.IndexOf(".")); i++)
            {
                tb.Text = tb.Text + "0";
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}